package cn.lyq.card.ui.customer

import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.src.Status
import cn.lyq.card.manager.UserManager
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_customer_service_link.*


class CustomerSLinkActivity : BaseActivity() {


    private val mModel by lazy {
        getVM(CustomerVM::class.java)
    }

    override fun getLayoutId(): Int {
        return R.layout.act_customer_service_link
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_link_customer_service)
    }

    override fun initListener() {
        super.initListener()
        tvCustomerServicePhone.setClick {
            if (UserManager.customer == null) {
                getCustomer()
            } else {
                CustomerSPhoneFg().show(supportFragmentManager, "CustomerSPhoneFg")
            }
        }
        tvCommonQuestion.setClick {
            startActivity(CommonQuestionActivity::class.java)
        }
    }

    private fun getCustomer() {
        mModel.getAboutUs().observe(this, Observer {
            when (it?.status) {
                Status.LOADING -> showLoading()
                Status.SUCCESS -> {
                    it.requireData().telephone?.let { tel ->
                        UserManager.customer = tel
                        CustomerSPhoneFg().show(supportFragmentManager, "CustomerSPhoneFg")
                    }
                    dismissLoading()
                }
                Status.ERROR -> {
                    dismissLoading()
                    ToastUtils.showShort(it.message.getMsg())
                }
            }
        })

    }
}